import { deepClone } from '@/utils'
import {getInfo, getMenu} from '@/api/user'
import { constantRoutes ,baseRouter} from '@/router'
import store from "@/store";

// 根据用户权限判断路由是否有权限 title=name
function hasPermission (permissionRoutes, route) {
  if (route.meta && route.meta.title) {
    return filterPermissions (permissionRoutes, route)
  } else {
    return false
  }
}
export function filterPermissions (permissionRoutes, route) {

  let flag = false
  for (let i = 0; i < permissionRoutes.length ; i++) {
    flag = permissionRoutes[i].name == route.meta.title
    if (flag){
      return true
    }
    if (permissionRoutes[i].children && permissionRoutes[i].children.length >0 && !flag ) {
      flag = filterPermissions(permissionRoutes[i].children, route)
    }else {

    }
  }
  return flag;
}


// 根据用户角色过滤掉无权限的路由配置
export function filterRoutes (srcRoutes, permissionRoutes) {
  const res = []
  srcRoutes.forEach(route => {
    let tmp = deepClone(route)
    if (hasPermission(permissionRoutes, tmp) ) {
      if (tmp.children) {
        tmp.children = filterRoutes(tmp.children, permissionRoutes)
      }
      if (!tmp.meta) {
        Object.assign(tmp, { meta: { auth: true } })
      } else {
        Object.assign(tmp.meta, { auth: true })
      }
      res.push(tmp)
    }
  })
  return res
}


const state = {
  routes: [],
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.routes =routes
  }
}

const actions = {
  // 根据角色添加路由
  generateRoutes ({ commit }) {
    return new Promise(resolve => {
      // 可以发请求获取菜单 permission 的值
      //请求接口 /api/rbac/operator/permissions
     /* getMenu().then(response => {
        // console.log('userInfo response', response)
        const  routes  = response
        const accessedRoutes = filterRoutes(constantRoutes, routes)
        if (routes.length == 0){
          commit('SET_ROUTES', baseRouter)
          resolve(baseRouter)
        }else {
          commit('SET_ROUTES', accessedRoutes)
          resolve(accessedRoutes)

        }
      }).catch(error => {
        reject(error)
      })*/
      const routes = [
        {
          "operations": [
            {
              "id": 4,
              "name": "查看"
            },
            {
              "id": 5,
              "name": "查询"
            },
            {
              "id": 6,
              "name": "编辑"
            },
            {
              "id": 7,
              "name": "上下架"
            },
            {
              "id": 9,
              "name": "导出"
            }
          ],
          "children": [],
          "name": "内容管理",
          "id": 1
        },
        {
          "children": [
            {
              "operations": [
                {
                  "id": 4,
                  "name": "查看"
                },
                {
                  "id": 5,
                  "name": "查询"
                },
                {
                  "id": 6,
                  "name": "编辑"
                },
                {
                  "id": 8,
                  "name": "新增"
                },
                {
                  "id": 9,
                  "name": "导出"
                },
                {
                  "id": 10,
                  "name": "删除"
                }
              ],
              "name": "运营标签",
              "id": 3
            }
          ],
          "name": "标签管理",
          "id": 2
        }
      ]
      const accessedRoutes = filterRoutes(constantRoutes, routes)
      if (routes.length == 0){
        commit('SET_ROUTES', baseRouter)
        resolve(baseRouter)
      }else {
        commit('SET_ROUTES', accessedRoutes)
        resolve(accessedRoutes)

      }
    })
  },
  updateRoutes ({ commit }) {
    commit('SET_ROUTES', [])
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
